package cn.wildfire.chat.app.extendsion.classes;

import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bilibili.boxing.loader.IBoxingCallback;
import com.bilibili.boxing.loader.IBoxingMediaLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

/**
*  Create by HEKE, 2019/9/28 12:00
*  TODO 自定义盒子加载
*/
public class BoxingGlideLoader implements IBoxingMediaLoader {

    @Override
    public void displayThumbnail(@NonNull ImageView img, @NonNull String absPath, int width, int height) {
        String path = "file://" + absPath;
        try {
           RequestBuilder<Bitmap> request = Glide.with(img.getContext()).asBitmap().load(path).apply(new RequestOptions().
                   placeholder(R.mipmap.avatar_def).centerCrop());
           request.submit(width,height);
           request.listener(new RequestListener<Bitmap>() {
               @Override
               public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                   img.setBackgroundResource(R.mipmap.default_header);
                   return false;
               }

               @Override
               public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                   if(target!=null){
                       img.setImageBitmap(resource);
                   }
                   return false;
               }
           }).into(img);
        } catch(IllegalArgumentException ignore) {
        }

    }

    @Override
    public void displayRaw(@NonNull final ImageView img, @NonNull String absPath, int width, int height, final IBoxingCallback callback) {
        String path = "file://" + absPath;
        RequestBuilder<Bitmap> request = Glide.with(img.getContext()).asBitmap()
                .load(path);
        if (width > 0 && height > 0) {
            request.submit(width,height);
        }
        request.listener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                if (callback != null) {
                    callback.onFail(e);
                    return true;
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                if (resource != null && callback != null) {
                    img.setImageBitmap(resource);
                    callback.onSuccess();
                    return true;
                }
                return false;
            }

        }).into(img);

    }

}